package com.chiffoyqc.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chiffoyqc.config.URLConfig;
import com.chiffoyqc.factory.ReturnCodeFactory;
import com.chiffoyqc.model.MeterAdd;
import com.chiffoyqc.model.MeterData_Rep;
import com.chiffoyqc.model.MeterData_Req;
import com.chiffoyqc.service.Interface.IHandleMeterData;
import com.chiffoyqc.utils.HttpHelper;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2018/7/12.
 * 7.8. 获取表具信息
 */
@Service
public class MeterDataService implements IHandleMeterData {
    @Override
    public Map<String, Object> handleRequestMessage(String postData) {

//        MeterData_Req meterData_req = JSON.parseObject(postData, MeterData_Req.class);
//        String postParams = JSON.toJSONString(meterData_req);
        System.out.println("获取表具信息 传给zsy参数：" + postData);
        String result = HttpHelper.sendPostForNormal(URLConfig.meterData, postData, "utf-8");
        System.out.println("获取表具信息 zsy返回的结果："+ result);
        if(!result.equals("") ){
            Map<String,Object> dataMap = new HashMap<>();
            JSONObject jsonObject = JSON.parseObject(result);
            /**组织returnCode returnMsg*/
            dataMap = ReturnCodeFactory.createReturnCodeMap(dataMap,jsonObject);


            /**组织其他数据*/
            JSONArray paramsArray = jsonObject.getJSONArray("param");
            if(paramsArray != null && paramsArray.size() != 0){
                MeterData_Rep meterData_rep;
                List<MeterData_Rep> list = new LinkedList<>();
                for(int i = 0 ,len = paramsArray.size(); i < len ; i++){
                    meterData_rep = paramsArray.getObject(i, MeterData_Rep.class);
                    list.add(meterData_rep);
                }

                dataMap.put("param",JSON.toJSONString(list));
            }



            return dataMap;
        }


        return null;
    }
}
